package com.omt.tarjimdinek.conversation;

import static com.omt.tarjimdinek.conversation.ConversationHolder.getCurrentConversation;

import java.util.Iterator;

import javax.faces.context.ExceptionHandler;
import javax.faces.event.ExceptionQueuedEvent;

import org.omnifaces.exceptionhandler.FullAjaxExceptionHandler;
import org.springframework.dao.DataAccessException;

import com.omt.tarjimdinek.conversation.ConversationManager;
import com.omt.tarjimdinek.util.ExceptionUtil;
import com.omt.tarjimdinek.util.MessageUtil;

/**
 * Exception handling is configured here, in web.xml (see error-page tag) and in faces-config.xml.
 */
public class ConversationAwareExceptionHandler extends FullAjaxExceptionHandler {

    public ConversationAwareExceptionHandler(ExceptionHandler wrapped) {
        super(wrapped);
    }

    @Override
    public void handle() {
        Iterator<ExceptionQueuedEvent> unhandledExceptionQueuedEvents = getUnhandledExceptionQueuedEvents().iterator();

        if (unhandledExceptionQueuedEvents.hasNext()) {
            Throwable exception = unhandledExceptionQueuedEvents.next().getContext().getException();

            // nice message
            MessageUtil.getInstance().error(exception);

            if (getCurrentConversation() != null && ExceptionUtil.isCausedBy(exception, DataAccessException.class)) {
                // DATA ACCESS EXCEPTION
                // As per JPA spec it is safer to do not reuse the entity manager.
                // Therefore, we end the conversation.
                ConversationManager.getInstance().endCurrentConversation();
                // fall through, the parent will display the proper view.
            }
        }
        super.handle();
    }
}
